package.cpath = package.cpath .. ';C:/Users/win/AppData/Roaming/JetBrains/IdeaIC2020.3/plugins/intellij-emmylua/classes/debugger/emmy/windows/x86/?.dll'
local dbg = require('emmy_core')
dbg.tcpConnect('localhost', 9966)

package.path = package.path .. ";C:/Users/win/Documents/_lua/util/?.lua"

local Array = require("Array")
math.randomseed(os.time())

function Test1()
    local arr = Array.new()
    assert(0 == arr:GetCount())

    arr:Add(1)
    assert(1 == arr:GetCount())
    arr:Add(2)
    assert(2 == arr:GetCount())
    arr:Add(3)
    assert(3 == arr:GetCount())
    arr:Add(4)
    assert(1 == arr[1])
    assert(2 == arr:GetAt(2))
    assert(3 == arr[3])
    assert(4 == arr:GetAt(4))

    arr:InsertAt(2, 1.5) --中间
    assert(5 == arr:GetCount())
    assert(1 == arr[1])
    assert(1.5 == arr:GetAt(2))
    assert(2 == arr[3])
    assert(3 == arr:GetAt(4))
    assert(4 == arr:GetAt(5))

    arr:Remove(1.5) --中间
    assert(4 == arr:GetCount())
    assert(1 == arr[1])
    assert(2 == arr[2])
    assert(3 == arr:GetAt(3))
    assert(4 == arr:GetAt(4))

    arr:Remove(1) --头
    assert(3 == arr:GetCount())
    assert(2 == arr[1])
    assert(3 == arr:GetAt(2))
    assert(4 == arr:GetAt(3))

    arr:Remove(4) --尾
    assert(2 == arr:GetCount())
    assert(2 == arr[1])
    assert(3 == arr:GetAt(2))

    arr:InsertAt(1, 1) --头
    assert(3 == arr:GetCount())
    assert(1 == arr[1])
    assert(2 == arr[2])
    assert(3 == arr:GetAt(3))

    arr:InsertAt(4, 4) --尾
    assert(4 == arr:GetCount())
    assert(1 == arr[1])
    assert(2 == arr[2])
    assert(3 == arr:GetAt(3))
    assert(4 == arr:GetAt(4))

    arr:RemoveAt(2) --中间
    assert(3 == arr:GetCount())
    assert(1 == arr[1])
    assert(3 == arr:GetAt(2))
    assert(4 == arr:GetAt(3))

    arr:RemoveAt(1) --头
    assert(2 == arr:GetCount())
    assert(3 == arr:GetAt(1))
    assert(4 == arr:GetAt(2))

    arr:RemoveAt(2) --尾
    assert(1 == arr:GetCount())
    assert(3 == arr:GetAt(1))

    arr:Add(4)
    arr:Add(5)
    arr:Clear()
    assert(0 == arr:GetCount())
    assert(nil == arr:GetAt(1))
    assert(nil == arr[2])

    arr:Add(1)
    arr:Add(2)
    arr:Add(3)
    arr:Add(4)

    assert(1 == arr:IndexOf(1))
    assert(2 == arr:IndexOf(2))
    assert(3 == arr:IndexOf(3))
    assert(4 == arr:IndexOf(4))
    assert(-1 == arr:IndexOf(5))

    assert(1 == arr:LastIndexOf(1))
    assert(2 == arr:LastIndexOf(2))
    assert(3 == arr:LastIndexOf(3))
    assert(4 == arr:LastIndexOf(4))
    assert(-1 == arr:LastIndexOf(5))

    assert(arr:Contains(1))
    assert(arr:Contains(2))
    assert(arr:Contains(3))
    assert(arr:Contains(4))
    assert(not arr:Contains(5))
    assert(4 == arr:GetCount())

    local findFunc = function(index, item)
        return item == 2
    end
    assert(2,2 == arr:Find(findFunc))
    assert(2,2 == arr:FindLast(findFunc))

    findFunc = function(index, item)
        return false
    end
    assert(-1, nil == arr:Find(findFunc))
    assert(-1, nil == arr:FindLast(findFunc))

    arr:Reverse()
    assert(4 == arr:GetCount())
    assert(4 == arr:LastIndexOf(1))
    assert(3 == arr:LastIndexOf(2))
    assert(2 == arr:LastIndexOf(3))
    assert(1 == arr:LastIndexOf(4))
    assert(-1 == arr:LastIndexOf(5))
    arr:Add(1)

    arr:Sort(function(a, b)
        return a < b
    end)
    assert(5 == arr:GetCount())
    assert(1 == arr:IndexOf(1))
    assert(3 == arr:IndexOf(2))
    assert(4 == arr:IndexOf(3))
    assert(5 == arr:IndexOf(4))
    assert(-1 == arr:IndexOf(5))

    for k, v in arr:GetIterator() do
        print(k, ":", v)
    end

    arr:Remove(1)
    arr:Add(5)
    arr:Add(6)
    arr:Add(7)

    arr:RemoveRange(1, 2) --头
    assert(5 == arr:GetCount())
    assert(3 == arr:GetAt(1))
    assert(4 == arr:GetAt(2))
    assert(5 == arr:GetAt(3))
    assert(6 == arr:GetAt(4))
    assert(7 == arr:GetAt(5))

    arr:RemoveRange(4, 2) --尾
    assert(3 == arr:GetCount())
    assert(3 == arr:GetAt(1))
    assert(4 == arr:GetAt(2))
    assert(5 == arr:GetAt(3))

    arr:Add(6)
    arr:Add(7)
    assert(5 == arr:GetCount())
    arr:RemoveRange(4, 3) --尾
    assert(3 == arr:GetCount())
    assert(3 == arr:GetAt(1))
    assert(4 == arr:GetAt(2))
    assert(5 == arr:GetAt(3))

    arr:Add(6)
    arr:Add(7)
    assert(5 == arr:GetCount())
    arr:RemoveRange(2, 3) --中间
    assert(2 == arr:GetCount())
    assert(3 == arr:GetAt(1))
    assert(7 == arr:GetAt(2))

    arr:Clear(Array.ClearTypeEnum.Keep)
    assert(0 == arr:GetCount())
    assert(nil == arr:GetAt(1))
    assert(nil == arr:GetAt(2))

    arr:Add(1)
    arr:Add(2)
    arr:Add(3)
    arr:Add(4)
    arr:Add(5)
    assert(5 == arr:GetCount())
    assert(5 == arr:GetLastAt(1))
    assert(4 == arr:GetLastAt(2))
    assert(3 == arr:GetLastAt(3))

    assert(true,5 == arr:RemoveLastAt(1))
    assert(4 == arr:GetCount())
    assert(true,4 == arr:RemoveLastAt(1))
    assert(3 == arr:GetCount())
end
Test1()


